1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.lang3.builder;
18  
19  import static org.junit.Assert.assertEquals;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  
24  import org.apache.commons.lang3.builder.ToStringStyleTest.Person;
25  import org.junit.After;
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  /**
30   * Unit tests {@link org.apache.commons.lang3.builder.ToStringStyle}.
31   *
32   * @version $Id$
33   */
34  public class StandardToStringStyleTest {
35  
36      private final Integer base = Integer.valueOf(5);
37      private final String baseStr = "Integer";
38      
39      private static final StandardToStringStyle STYLE = new StandardToStringStyle();
40      
41      static {
42          STYLE.setUseShortClassName(true);
43          STYLE.setUseIdentityHashCode(false);
44          STYLE.setArrayStart("[");
45          STYLE.setArraySeparator(", ");
46          STYLE.setArrayEnd("]");
47          STYLE.setNullText("%NULL%");
48          STYLE.setSizeStartText("%SIZE=");
49          STYLE.setSizeEndText("%");
50          STYLE.setSummaryObjectStartText("%");
51          STYLE.setSummaryObjectEndText("%");
52      }
53      
54      @Before
55      public void setUp() throws Exception {
56          ToStringBuilder.setDefaultStyle(STYLE);
57      }
58  
59      @After
60      public void tearDown() throws Exception {
61          ToStringBuilder.setDefaultStyle(ToStringStyle.DEFAULT_STYLE);
62      }
63  
64      //----------------------------------------------------------------
65      
66      @Test
67      public void testBlank() {
68          assertEquals(baseStr + "[]", new ToStringBuilder(base).toString());
69      }
70  
71      @Test
72      public void testAppendSuper() {
73          assertEquals(baseStr + "[]", new ToStringBuilder(base).appendSuper("Integer@8888[]").toString());
74          assertEquals(baseStr + "[%NULL%]", new ToStringBuilder(base).appendSuper("Integer@8888[%NULL%]").toString());
75          
76          assertEquals(baseStr + "[a=hello]", new ToStringBuilder(base).appendSuper("Integer@8888[]").append("a", "hello").toString());
77          assertEquals(baseStr + "[%NULL%,a=hello]", new ToStringBuilder(base).appendSuper("Integer@8888[%NULL%]").append("a", "hello").toString());
78          assertEquals(baseStr + "[a=hello]", new ToStringBuilder(base).appendSuper(null).append("a", "hello").toString());
79      }
80      
81      @Test
82      public void testObject() {
83          final Integer i3 = Integer.valueOf(3);
84          final Integer i4 = Integer.valueOf(4);
85          assertEquals(baseStr + "[%NULL%]", new ToStringBuilder(base).append((Object) null).toString());
86          assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(i3).toString());
87          assertEquals(baseStr + "[a=%NULL%]", new ToStringBuilder(base).append("a", (Object) null).toString());
88          assertEquals(baseStr + "[a=3]", new ToStringBuilder(base).append("a", i3).toString());
89          assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base).append("a", i3).append("b", i4).toString());
90          assertEquals(baseStr + "[a=%Integer%]", new ToStringBuilder(base).append("a", i3, false).toString());
91          assertEquals(baseStr + "[a=%SIZE=0%]", new ToStringBuilder(base).append("a", new ArrayList<Object>(), false).toString());
92          assertEquals(baseStr + "[a=[]]", new ToStringBuilder(base).append("a", new ArrayList<Object>(), true).toString());
93          assertEquals(baseStr + "[a=%SIZE=0%]", new ToStringBuilder(base).append("a", new HashMap<Object, Object>(), false).toString());
94          assertEquals(baseStr + "[a={}]", new ToStringBuilder(base).append("a", new HashMap<Object, Object>(), true).toString());
95          assertEquals(baseStr + "[a=%SIZE=0%]", new ToStringBuilder(base).append("a", (Object) new String[0], false).toString());
96          assertEquals(baseStr + "[a=[]]", new ToStringBuilder(base).append("a", (Object) new String[0], true).toString());
97      }
98  
99      @Test
100     public void testPerson() {
101         final Person p = new Person();
102         p.name = "Suzy Queue";
103         p.age = 19;
104         p.smoker = false;
105         final String pBaseStr = "ToStringStyleTest.Person";
106         assertEquals(pBaseStr + "[name=Suzy Queue,age=19,smoker=false]", new ToStringBuilder(p).append("name", p.name).append("age", p.age).append("smoker", p.smoker).toString());
107     }
108 
109     @Test
110     public void testLong() {
111         assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(3L).toString());
112         assertEquals(baseStr + "[a=3]", new ToStringBuilder(base).append("a", 3L).toString());
113         assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base).append("a", 3L).append("b", 4L).toString());
114     }
115 
116     @Test
117     public void testObjectArray() {
118         Object[] array = new Object[] {null, base, new int[] {3, 6}};
119         assertEquals(baseStr + "[[%NULL%, 5, [3, 6]]]", new ToStringBuilder(base).append(array).toString());
120         assertEquals(baseStr + "[[%NULL%, 5, [3, 6]]]", new ToStringBuilder(base).append((Object) array).toString());
121         array = null;
122         assertEquals(baseStr + "[%NULL%]", new ToStringBuilder(base).append(array).toString());
123         assertEquals(baseStr + "[%NULL%]", new ToStringBuilder(base).append((Object) array).toString());
124     }
125 
126     @Test
127     public void testLongArray() {
128         long[] array = new long[] {1, 2, -3, 4};
129         assertEquals(baseStr + "[[1, 2, -3, 4]]", new ToStringBuilder(base).append(array).toString());
130         assertEquals(baseStr + "[[1, 2, -3, 4]]", new ToStringBuilder(base).append((Object) array).toString());
131         array = null;
132         assertEquals(baseStr + "[%NULL%]", new ToStringBuilder(base).append(array).toString());
133         assertEquals(baseStr + "[%NULL%]", new ToStringBuilder(base).append((Object) array).toString());
134     }
135 
136     @Test
137     public void testLongArrayArray() {
138         long[][] array = new long[][] {{1, 2}, null, {5}};
139         assertEquals(baseStr + "[[[1, 2], %NULL%, [5]]]", new ToStringBuilder(base).append(array).toString());
140         assertEquals(baseStr + "[[[1, 2], %NULL%, [5]]]", new ToStringBuilder(base).append((Object) array).toString());
141         array = null;
142         assertEquals(baseStr + "[%NULL%]", new ToStringBuilder(base).append(array).toString());
143         assertEquals(baseStr + "[%NULL%]", new ToStringBuilder(base).append((Object) array).toString());
144     }
145 
146 }